(import (except (rnrs base) error map)
        (only (guile)
              lambda* λ
              simple-format
              error
              even?
              odd?)
        ;; SRFI 1: list procedures
        (srfi srfi-1)
        ;; SRFI-27 for random number utilities
        (srfi srfi-27)
        ;; SRFI 43: vectors
        (srfi srfi-43)
        ;; SRFI 69: hash tables
        (srfi srfi-69)

        (ice-9 pretty-print)
        (ice-9 match)

        ;; purely functional data structures
        (pfds sets)
        (random-utils)
        (statistics))


(define who-died?
  (λ (events)
    (let ([index (list-index (λ (event) (= event 1)) events)])
      (cond
       [(= (remainder index 2) 0)
        'player1]
       [else  ;; 2
        'player2]))))


(define rng (make-random-integer-generator))


(define generated
  (do-n-times
   (λ ()
     (generate-random-list 6
                           (alist->hash-table
                            '((0 . 5) (1 . 1)))
                           #:rng rng))
   1000))


;; (generate-random-list 6
;;                       (alist->hash-table
;;                        '((0 . 5) (1 . 1)))
;;                       #:rng rng)

;; (pretty-print generated)

(pretty-print
 (hash-table->alist
  (count-things generated
                (λ (events)
                  (list-index (λ (event) (= event 1)) events))
                (λ (events) 1)
                0)))

(pretty-print
 (hash-table->alist
  (count-things generated
                (λ (events)
                  (if (even? (list-index (λ (event) (= event 1)) events))
                      'even
                      'odd))
                (λ (events) 1)
                0)))

(pretty-print
 (hash-table->alist
  (count-things generated
                who-died?
                (λ (events) 1)
                0)))

;; (pretty-print
;;  (hash-table->alist
;;   (count-things (do-n-times (λ () (rng 2))
;;                             1000)
;;                 (λ (n) n)
;;                 (λ (n) 1)
;;                 0)))
